package wec_wallet.ebo.com.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.bean.WalletDateBean;
import wec_wallet.ebo.com.util.StringUtil;

/**
 * Created by Administrator on 2018/4/3.
 */

public class SpinnerPopupWindow extends PopupWindow {
    private LayoutInflater inflater;
    private ListView mListView;
    private List<WalletDateBean> list;
    private MyAdapter mAdapter;
    Context context;

    public SpinnerPopupWindow(Context context, List<WalletDateBean> list, AdapterView
            .OnItemClickListener
            clickListener) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.list = list;
        init(clickListener);
    }

    private void init(AdapterView.OnItemClickListener clickListener) {
        View view = inflater.inflate(R.layout.activity_spiner_window_layout, null);
        setContentView(view);
        setWidth(LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
        setHeight(LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00);
        setBackgroundDrawable(dw);
        mListView = view.findViewById(R.id.listview);
        mListView.setAdapter(mAdapter = new MyAdapter());
        mListView.setOnItemClickListener(clickListener);
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.activity_spinner_wallet_item, null);
                holder.tvName = convertView.findViewById(R.id.tv_itemSpinner_coin);
                holder.img = convertView.findViewById(R.id.img_itemSpinner_coin);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvName.setText(list.get(position).getWallet_name());
//            String pic = list.get(position).getIcon();
//            if (StringUtil.isStringEmpty(pic)) {
////                holder.img.setImageResource(R.drawable.ic_logout);
//            } else {
//                Glide.with(context).load(pic).into(holder.img);
//            }
//            holder.img.setImageResource(R.drawable.ic_logout);

            return convertView;
        }
    }

    private class ViewHolder {
        private TextView tvName;
        private ImageView img;
    }

}
